package com.example.baiduapi.Controller;

import com.alibaba.fastjson.JSON;
import com.example.baiduapi.pojo.JFData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import static com.example.baiduapi.Util.PostUtil.postRequest;

@Controller
public class testController2 {

    @Value("${server.HTTPURL}")
    private String HTTPURL;

    @Async
    @GetMapping("/sssssss")
    public String test2() throws Exception {
        for (int i = 0; i < 3; i++) {
            request(i+"");
        }
        return "test";
    }
    @Async
    @GetMapping("/getFile")
    @ResponseBody
    public String getFile() throws Exception {
        List list = new ArrayList<>();
        File folder = new File("D:\\IDEADEMO\\test-demo\\BaiDuAPI\\src\\main\\resources\\static\\DownLoadFile\\image");
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        list.add(file.getName());
                    }
                }
            } else {
                System.out.println("文件夹中没有文件");
            }
        } else {
            System.out.println("指定路径不是一个文件夹");
        }

        String result = JSON.toJSONString(list);
        System.out.println(result);
return result;
    }

    @Async
    public void request(String i) throws Exception {
        Thread.sleep(3000);
        String body = "{}";

        while (true) {
            System.out.println("测试次数："+i);
            String s = postRequest(this.HTTPURL+"/Login?username=123123&password=123123&Messagestate=2", body);
              System.out.println(s);
        }

    }

    @PostMapping("pushChargeRefund2")
    @ResponseBody
    public String pushCancellation(@RequestBody List<JFData> dtos) {
        System.out.println("dtos = " + dtos);
        return dtos.toString();
    }

    public static void main(String[] args) throws IOException {
        while (true){
            String url = "http://117.50.175.234:9001/api/register";
            String username = "testUser12311";
            String password = "testpass";
            String postData = "username=" + username + "&password=" + password;

            URL urlObj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);

            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(postData.getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
            outputStream.close();

            int responseCode = connection.getResponseCode();

            BufferedReader reader;
            if (responseCode == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            } else {
                reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            }

            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            System.out.println("Response Code: " + responseCode);
            System.out.println("Response: " + response.toString());
        }
    }
}
